<template>
	<view>
		<my-search @myclick = "goSearch"></my-search>
		
		<view class="scroll-view-container">
			<!-- 左侧的滚动视图区域 -->
			<scroll-view class="left-scroll-view" scroll-y="true" :style="{height: windowHeight + 'px'}">
				<block v-for="(item, index) in cateList" :key="item.cat_id">
					<view :class="['left-scroll-view-item', index == active ? 'active' : '']"
						@click="activeHandle(index)">{{item.cat_name}}</view>
				</block>
			</scroll-view>

			<!-- 右侧的滚动视图区域 -->
			<scroll-view scroll-y="true" class="right-scroll-view" :style="{height:windowHeight+'px'}"
				:scroll-top="scrollTop">
				<view class="right-scroll-view-item" v-for="(item2,index2) in cateList2" :key="item2.cat_id">
					<!-- 二级分类 -->
					<view class="cate-title">/ {{item2.cat_name}} / </view>
					<!-- 三级分类 -->
					<view class="cate-list">
						<view v-for="(item3,index3) in item2.children" :key="item3.cat_id" class="cate-item" @click="getGoodsList(item3)">
							<img :src="item3.cat_icon" alt="" />
							<view>{{item3.cat_name}}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 屏幕高度
				windowHeight: 0,
				// 分类数据列表
				cateList: [],
				// 选中的样式,默认为0
				active: 0,
				// 二级分类列表
				cateList2: [],
				// 滚动条的值
				scrollTop: 0
			};
		},
		methods: {
			// 设置选中效果以及渲染右侧界面
			async activeHandle(index) {
				this.active = index

				let result = await uni.$http.get('/api/public/v1/categories')
				if (result.statusCode == 200) {
					this.cateList2 = result.data.message[index].children
					console.log(this.cateList2)
				}

				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},

			// 获取分类列表数据
			async getCateList() {
				let result = await uni.$http.get('/api/public/v1/categories')
				if (result.statusCode == 200) {
					this.cateList = result.data.message

					// 为二级分类赋初值
					this.cateList2 = result.data.message[0].children
				}
			},
			
			// 点击三级分类跳转到商品列表
			getGoodsList(item3){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_id
				})
			},
			
			// 搜索组件的点击事件
			goSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		},
		onLoad() {
			// 获取系统信息
			const sysInfo = uni.getSystemInfoSync()
			// 减去搜索框的高度
			this.windowHeight = sysInfo.windowHeight - 50

			this.getCateList()
		},
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		// 左侧滑动
		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				background-color: #F7F7F7;
				line-height: 60px;
				text-align: center;
				font-size: 12px;
				position: relative;

				&.active {
					background-color: #FFFFFF;

					&::before {
						content: '';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #C00000;
						position: absolute;
						top: 50%;
						left: 0%;
						transform: translateY(-50%);
					}
				}
			}
		}
	}

	// 右侧内容
	.right-scroll-view {

		// 右侧二级分类标题
		.cate-title {
			font-size: 12px;
			font-weight: bold;
			text-align: center;
			padding: 15px 0;
		}

		// 右侧三级分类容器
		.cate-list {
			display: flex;
			flex-wrap: wrap;

			.cate-item {
				width: 33.33%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-bottom: 10px;

				img {
					width: 60px;
					height: 60px;
				}

				text {
					font-size: 12px;
				}
			}
		}
	}
</style>